<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>类别列表</span>
        <el-button type="primary" @click="addDialog = true">添加分类</el-button>
      </div>
      <el-table :data="list" stripe style="width: 100%" border>
        <el-table-column prop="id" label="ID" width="180"> </el-table-column>
        <el-table-column prop="cate_name" label="分类名称" width="180"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot:default="obj">
            <el-button size="mini" @click="showEditDialog(obj.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="del(obj.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加的对话框-->
    <el-dialog title="添加分类" :before-close="cancel" :visible.sync="addDialog" width="500px">
      <el-form label-width="80px" :model="addData" :rules="rules" ref="addForm">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input autocomplete="off" v-model="addData.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input autocomplete="off" v-model="addData.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="addCategory">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 编辑的对话框-->
    <el-dialog title="修改分类" :visible.sync="editDialog" width="500px">
      <el-form label-width="80px" ref="editForm" :model="editData" :rules="rules">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input autocomplete="off" v-model="editData.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input autocomplete="off" v-model="editData.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialog = false">取 消</el-button>
        <el-button type="primary" @click="updateCategory">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCategoryAPI,
  addCategoryAPI,
  deleteCategoryAPI,
  updateCategoryAPI
} from '@/api/category'
export default {
  name: 'CateList',
  data() {
    return {
      list: [],
      addDialog: false,
      editDialog: false,
      addData: {
        cate_name: '',
        cate_alias: ''
      },
      // 编辑表单的数据
      editData: {},
      rules: {
        cate_name: [
          { required: true, message: '分类名必填', trigger: 'blur' },
          { min: 1, max: 10, message: '分类名必须为1-10位', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '分类别名必填', trigger: 'blur' },
          // {1,15} 不能写成 {1, 15}，不能有空格
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '只能是长度1~15位的字母数字组合',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getCategory()
  },
  // 获取分类
  methods: {
    async getCategory() {
      const { data: res } = await getCategoryAPI()
      if (res.code === 0) {
        this.list = res.data
      }
    },
    // 添加分类方法
    addCategory() {
      this.$refs.addForm.validate(async valid => {
        if (valid) {
          const { data: res } = await addCategoryAPI(this.addData)
          if (res.code === 0) {
            this.$message.success(res.message)
            // 添加完成，重置表单
            this.$refs.addForm.resetFields()
            // 弹框隐藏
            this.addDialog = false
            // 重新获取数据内容
            this.getCategory()
          } else {
            this.$message.error(res.message)
          }
        }
      })
    },
    // 取消添加的方法
    cancel() {
      this.$refs.addForm.resetFields()
      this.addDialog = false
    },
    // 删除的方法
    del(id) {
      this.$confirm('确定要删除吗？', '提示', {
        type: 'warning',
        callback: async r => {
          // console.log(r) // r 的值可能是 confirm、cancel、close
          if (r === 'confirm') {
            // 说明用户点击了确定，这个时候可以调用 API方法进行删除了
            const { data: res } = await deleteCategoryAPI(id)
            if (res.code === 0) {
              this.$message.success(res.message)
              this.getCategory() // 调用获取分类的方法，重新获取数据，重新渲染到页面
            } else {
              this.$message.error(res.message)
            }
          }
        }
      })
    },
    // 显示编辑对话框的方法
    showEditDialog(row) {
      this.editData = { ...row }
      this.editDialog = true
      // console.log(row)
    },
    // 完成更新分类的方法
    updateCategory() {
      this.$refs.editForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await updateCategoryAPI(this.editData)
        if (res.code === 0) {
          this.$message.success(res.message)
          this.getCategory()
          this.editDialog = false
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.clearfix {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
